package com.fresh.conf;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConf {

    //自定义序列化格式
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public TopicExchange exchangeOrder() {
        return new TopicExchange("exchangeOrder");
    }

    @Bean
    public Queue queueOrder() {
        Map<String, Object> arguments = new HashMap<>();
        //设置队列统一过期时间
        arguments.put("x-message-ttl", 3 * 1000);
        //设置死信交换机
        arguments.put("x-dead-letter-exchange", "exchangeOrderDLX");
        //设置死信路由key
        arguments.put("x-dead-letter-routing-key", "orderDLX");

        return new Queue("queueOrder", true, false, false, arguments);
    }

    @Bean
    public Binding bindingQueueOrderToExchangeOrder(TopicExchange exchangeOrder, Queue queueOrder) {
        return BindingBuilder.bind(queueOrder).to(exchangeOrder).with("#.order.#");
    }

    //死信队列
    @Bean
    public TopicExchange exchangeOrderDLX() {
        return new TopicExchange("exchangeOrderDLX");
    }

    @Bean
    public Queue queueOrderDLX() {
        return new Queue("queueOrderDLX");
    }

    @Bean
    public Binding bindingQueueOrderDLXToExchangeOrderDLX(TopicExchange exchangeOrderDLX, Queue queueOrderDLX) {
        return BindingBuilder.bind(queueOrderDLX).to(exchangeOrderDLX).with("#.orderDLX.#");
    }


/*    //延迟队列
    @Bean
    public TopicExchange exchangeDelay() {
        TopicExchange exchangeDelay = new TopicExchange("exchangeDelay");
        //设置交换机启用延迟发送
        exchangeDelay.setDelayed(true);
        return exchangeDelay;
    }

    @Bean
    public Queue queueDelay() {
        return new Queue("queueDelay");
    }

    @Bean
    public Binding bindingQueueDelayToExchangeDelay(TopicExchange exchangeDelay, Queue queueDelay) {
        return BindingBuilder.bind(queueDelay).to(exchangeDelay).with("#.delay.#");
    }*/

}
